package com.openapi.protocol.decode.subPackageHandle.terminalPositionInfoUpload;

import com.openapi.data.JT808PackageData;
import com.openapi.data.JT808PushData;
import com.openapi.protocol.decode.subPackageHandle.SubPackageHandle;
import com.openapi.protocol.decode.subPackageHandle.SubPackageStatus;
import exsun.jt808.data.config.jt808.Jt808MsgIdConfig;
import exsun.jt808.data.config.jt808.Jt808ReceiveMsgTopic;
import exsun.jt808.data.parseUtil.GpsUtil;
import exsun.jt808.data.upload_data.GpsData;
import exsun.jt808.data.upload_data.jt808.TerminalPositionInfoUpload;
import exsun.jt808.data.util.parse.BitOperator;
import exsun.jt808.data.util.parse.DecoderUtils;
import exsun.jt808.data.util.parse.HexStrUtil;
import exsun.jt808.data.util.parse.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 定位信息批量上传
 */
@Component
public class TerminalPositionInfoUpload_0x0704 extends SubPackageHandle<JT808PackageData>
{
    private static final Logger logger = LoggerFactory.getLogger(TerminalPositionInfoUpload_0x0704.class);

    public JT808PushData decode(JT808PackageData data){
        try {
            SubPackageStatus status = super.append(data);
            String key = getPakKey(data);
            if(status == SubPackageStatus.complete){
                byte[] totalBytes = super.getAllData(data);
                TerminalPositionInfoUpload fileList = parse(data, totalBytes);
               ///推送整个数据体
                super.kafkaService.push(fileList);

               ///这里是遍历的gps
                for (GpsData gpsData : fileList.gpsDataList){
                    gpsData.mid = Jt808MsgIdConfig.gpsData;
                    gpsData.messageLength = data.messageLength;
                    gpsData.versionId = data.versionId;
                    gpsData.encryption = data.encryption;
                    gpsData.subcontracting = data.subcontracting;
                    gpsData.media = data.media;
                    gpsData.dvo = data.dvo;
                    gpsData.msgSerialNo = data.msgSerialNo;
                    gpsData.packageSize = data.packageSize;
                    gpsData.packageIndex = data.packageIndex;
                    gpsData.pte = data.pte;
                    gpsData.tpc = Jt808ReceiveMsgTopic.blindUpload;
//                    gpsData.pop.put(244,"10000000000000000000000000000000");
                    super.kafkaService.push(gpsData);
                }

                super.remove(key);
//                logger.info("定位数据批量上传接收完成！{}", data.dvo);00
            }
            return super.Response(data);
        }catch (Exception e){
            logger.error("定位数据批量上传:{}", e.getMessage());
        }
        return null;
    }

    @Override
    public String getPakKey(JT808PackageData data) {
        return data.dvo + "-" + data.getPackageSize();
    }


    public TerminalPositionInfoUpload parse(JT808PackageData data, byte[] body) {
//        数据项个数
        TerminalPositionInfoUpload obj = new TerminalPositionInfoUpload();
        super.setResultObj(obj, data, Jt808ReceiveMsgTopic.TerminalPositionInfoUpload);

        int pos = 0;
        int itemCount =  DecoderUtils.parseIntFromBytes(body,pos, 2);
        pos += 2;
        //位置数据类型
        int type = body[2];
        obj.setType(type);
        pos += 1;
        List<byte[]> posMsgs = new ArrayList<byte[]>();
        while (pos < body.length || posMsgs.size() < itemCount) {
            int itemLen = DecoderUtils.parseIntFromBytes(body, pos, 2);
            pos += 2;
            posMsgs.add(DecoderUtils.byteCopy(body, pos,itemLen));
            pos += itemLen;
        }
        for (byte[] posMsg : posMsgs) {
            if (type == 1){
                GpsData gpsData = GpsUtil.parseGps(posMsg,true);
                obj.gpsDataList.add(gpsData);
            }else if(type == 0){
                GpsData gpsData = GpsUtil.parseGps(posMsg,false);
                obj.gpsDataList.add(gpsData);
            }
        }
        return obj;
    }

}
